package com.dcivision.framework.web;

import java.sql.Connection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.ProtectedResource;


public class DefaultProtectedFilterHelper implements ProtectedFilterHelper{

  protected SessionContainer sessionContainer;
  protected Connection dbConn;
  protected List  protectedResourceList = null;

  public  DefaultProtectedFilterHelper ( SessionContainer sessionContainer, Connection dbConn){
    this.sessionContainer = sessionContainer;
    this.dbConn = dbConn;
  }
 
  public List getProtectedResourceList() {
    return protectedResourceList;
  }
  
  public void setProtectedResourceList(List protectedResourceList) {
    this.protectedResourceList = protectedResourceList;
  }
  
  public boolean isProtected(HttpServletRequest request)throws Exception {
    String navMode = (request.getParameter("navMode")==null)? "":request.getParameter("navMode");
    String opMode = (request.getParameter("opMode")==null)?"":request.getParameter("opMode");
    if (!Utility.isEmpty(this.getProtectedResourceList())) {
      for(int i = 0; i < this.getProtectedResourceList().size(); i++) {
        ProtectedResource protectedResource = (ProtectedResource)this.getProtectedResourceList().get(i);
        if (!Utility.isEmpty(protectedResource.getUrlPattern())) {
          // Compare query URL pattern with protected record definition. 
          // 1. If "NAV_MODE" is definition, check the request url, if contain this "NAV_MODE", return true.
          // 2. If "OP_MODE" is definition, check the request url, if contain this "OP_MODE", return true.
          // TODO: 3. If "OP_MODE" and "NAV_MODE" are not definition, ignor protected check or keep all operation to check? current is ignor choose.
          
          // Condition(1).
          if (!Utility.isEmpty(navMode) && !Utility.isEmpty(protectedResource.getNavMode()) && protectedResource.getNavMode().indexOf(navMode) > -1) {
            return true;
          }
          
          // Condition(2).
          if (!Utility.isEmpty(opMode) && !Utility.isEmpty(protectedResource.getOpMode()) && protectedResource.getOpMode().indexOf(opMode) > -1) {
            return true;
          }
        }
          /*
          if (Utility.isEmpty(protectedResource.getNavMode()) && Utility.isEmpty(protectedResource.getOpMode())) {
            return true;
          } else if (!Utility.isEmpty(protectedResource.getNavMode()) && !Utility.isEmpty(protectedResource.getOpMode())) {
            // NOTE: It is hard to understand underlying function. protectedResource.getOpMode()).indexOf("E") >= 0 ??????
            // If "navMode" is null, "opMode" is any character (e.g. 'D'), and protectedResource.getOpMode() is 'E', then ignor protected-process?
            if ((protectedResource.getNavMode()).indexOf(navMode) >= 0 && (protectedResource.getOpMode()).indexOf("E") >= 0) {
              return true;
            } 
          } else if (!Utility.isEmpty(protectedResource.getNavMode())) {
            if (protectedResource.getNavMode().indexOf(navMode) >= 0) {
              return true;
            } 
          } else if (!Utility.isEmpty(protectedResource.getOpMode())) {
            if (protectedResource.getOpMode().indexOf(opMode) >= 0) {
              return true;
            } 
          }
        } 
        */
      }
    } else {
      return false;
    }
    return false;
  }

  }
